package twice.class_digui;

public class Class15_CoinsWayNoLimit {
    public static int coinsWay(int[] arr, int aim) {
        if (arr == null || arr.length == 0 || aim < 0) {
            return 0;
        }
        return process(arr, 0, aim);
    }

    public static int process(int[] arr,int index,int rest){
        if (index == arr.length) { // 没钱了
            return rest == 0 ? 1 : 0;
        }
        int ways = 0;
        for(int zhang = 0; zhang*arr[index] <= rest;zhang++){
            ways += process(arr,index+1,rest - (zhang * arr[index]));
        }
        return ways;
    }

    public static int dp1(int[] arr,int aim){
        if (arr == null || arr.length == 0 || aim < 0) {
            return 0;
        }
        int N = arr.length;
        int[][] dp = new int[N+1][aim+1];
        dp[N][0] = 1;
        for(int i = arr.length - 1; i >= 0;i--){
            for (int rest = 0; rest <= aim;rest++){
                int ways = 0;
                for(int zhang = 0; zhang*arr[i] <= rest;zhang++){
                    ways += dp[i+1][rest - (zhang * arr[i])];
                }
                dp[i][rest] = ways;
            }

        }
        return dp[0][aim];
    }

    public static int dp2(int[] arr,int aim){
        if (arr == null || arr.length == 0 || aim < 0) {
            return 0;
        }
        int N = arr.length;
        int[][] dp = new int[N+1][aim+1];
        dp[N][0] = 1;
        for(int i = arr.length - 1; i >= 0;i--){
            for (int rest = 0; rest <= aim;rest++){
                dp[i][rest] = dp[i+1][rest];
                if(rest - arr[i] >= 0)
                dp[i][rest] += dp[i][rest-arr[i]];
            }

        }
        return dp[0][aim];
    }
    // 为了测试
    public static int[] randomArray(int maxLen, int maxValue) {
        int N = (int) (Math.random() * maxLen);
        int[] arr = new int[N];
        boolean[] has = new boolean[maxValue + 1];
        for (int i = 0; i < N; i++) {
            do {
                arr[i] = (int) (Math.random() * maxValue) + 1;
            } while (has[arr[i]]);
            has[arr[i]] = true;
        }
        return arr;
    }

    // 为了测试
    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // 为了测试
    public static void main(String[] args) {
        int maxLen = 10;
        int maxValue = 30;
        int testTime = 1000000;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr = randomArray(maxLen, maxValue);
            int aim = (int) (Math.random() * maxValue);
            int ans1 = coinsWay(arr, aim);
            int ans2 = dp1(arr, aim);
            int ans3 = dp2(arr, aim);
            if (ans1 != ans2 || ans1 != ans3
                    ) {
                System.out.println("Oops!");
                printArray(arr);
                System.out.println(aim);
                System.out.println(ans1);
                System.out.println(ans2);
                System.out.println(ans3);
                break;
            }
        }
        System.out.println("测试结束");
    }
}
